<?php
/**
 * 易优CMS
 * ============================================================================
 * 版权所有 2016-2028 海南赞赞网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.eyoucms.com
 * ----------------------------------------------------------------------------
 * 如果商业用途务必到官方购买正版授权, 以免引起不必要的法律纠纷.
 * ============================================================================
 * Author: 陈风任 <491085389@qq.com>
 * Date: 2019-06-21
 */

namespace app\admin\controller;

use think\Page;
use think\Db;
use think\Config;

class Level extends Base
{

    /**
     * 构造方法
     */
    public function __construct()
    {
        parent::__construct();
        /*会员中心数据表*/
        $this->users_db = Db::name('users');         // 会员信息表
        $this->users_level_db = Db::name('users_level');   // 会员等级表
        $this->users_money_db = Db::name('users_money');   // 会员充值表
        $this->users_type_manage_db = Db::name('users_type_manage');   // 会员等级表
        /*结束*/

        // 是否开启支付功能设置
        $UsersConfigData = getUsersConfigData('all');
        $this->assign('userConfig', $UsersConfigData);

        // 模型是否开启
        $channeltype_row = \think\Cache::get('extra_global_channeltype');
        $this->assign('channeltype_row', $channeltype_row);
    }

    /**
     *  列表
     */
    public function index()
    {
        // 会员级别
        $list = $this->users_level_db->where(['is_system' => 0])->order('level_value asc, level_id asc')->select();
        $this->assign('list', $list);

        // 会员升级期限
        $member_limit_arr = Config::get('global.admin_member_limit_arr');
        $this->assign('member_limit_arr', $member_limit_arr);

        // 会员升级产品分类
        $users_type = $this->users_type_manage_db->order('sort_order asc, type_id asc')->select();
        $this->assign('users_type', $users_type);

        return $this->fetch();
    }

    /**
     *  会员升级业务列表
     */
    public function upgrade_index()
    {
        // 查询条件
        $where = [
            'a.cause_type' => 0,
            'a.status' => ['IN', [2, 3]],
        ];

        // 订单号或会员名查询
        $keywords = input('keywords/s');
        if (!empty($keywords)) $where['a.order_number|b.username'] = array('LIKE', "%{$keywords}%");

        // 支付方式查询
        $pay_method = input('pay_method/s');
        if (!empty($pay_method)) $where['a.pay_method'] = $pay_method;

        // 会员级别查询
        $level_id = input('level_id/d');
        if (!empty($level_id)) $where['a.level_id'] = $level_id;

        // 时间检索条件
        $begin = strtotime(input('param.add_time_begin/s'));
        $end = input('param.add_time_end/s');
        !empty($end) && $end .= ' 23:59:59';
        $end = strtotime($end);
        // 时间检索
        if ($begin > 0 && $end > 0) {
            $where['a.add_time'] = array('between', "$begin, $end");
        } else if ($begin > 0) {
            $where['a.add_time'] = array('egt', $begin);
        } else if ($end > 0) {
            $where['a.add_time'] = array('elt', $end);
        }

        // 分页查询
        $count = $this->users_money_db->alias('a')->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')->where($where)->count();
        $Page = new Page($count, config('paginate.list_rows'));

        // 数据查询
        $list = $this->users_money_db->field('a.*, b.head_pic, b.username, b.nickname')
            ->alias('a')
            ->join('__USERS__ b', 'a.users_id = b.users_id', 'LEFT')
            ->where($where)
            ->order('a.moneyid desc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();
        foreach ($list as $key => $value) {
            $value['username'] = !empty($value['nickname']) ? $value['nickname'] : $value['username'];
            // 反序列化参数
            $value['cause'] = unserialize($value['cause']);
            $value['head_pic'] = get_head_pic($value['head_pic']);
            $list[$key] = $value;
        }
        $show = $Page->show();
        $this->assign('page', $show);
        $this->assign('list', $list);
        $this->assign('pager', $Page);

        // 会员等级列表
        $usersLevel = $this->users_level_db->field('level_id, level_name')->order('level_value asc')->getAllWithIndex('level_id');
        $this->assign('usersLevel', $usersLevel);

        // 支付状态
        $pay_status_arr = config('global.pay_status_arr');
        $this->assign('pay_status_arr', $pay_status_arr);

        // 是否开启文章付费
        $channelRow = Db::name('channeltype')->where('nid', 'in', ['article', 'download'])->getAllWithIndex('nid');
        foreach ($channelRow as &$val) {
            if (!empty($val['data'])) $val['data'] = json_decode($val['data'], true);
        }
        $this->assign('channelRow', $channelRow);

        return $this->fetch();
    }

    /**
     * 删除会员升级
     */
    public function upgrade_del()
    {
        if (IS_POST) {
            $id_arr = input('del_id/a');
            $id_arr = eyIntval($id_arr);
            if (!empty($id_arr)) {
                $result = Db::name('users_money')->field('order_number')
                    ->where([
                        'moneyid' => ['IN', $id_arr],
                        'cause_type' => 0,
                    ])->select();
                $order_number_list = get_arr_column($result, 'order_number');

                $r = Db::name('users_money')->where([
                    'moneyid' => ['IN', $id_arr],
                    'cause_type' => 0,
                ])
                    ->cache(true, null, "users_money")
                    ->delete();
                if ($r !== false) {
                    adminLog('删除会员升级记录：' . implode(',', $order_number_list));
                    $this->success('删除成功');
                }
            }
            $this->error('删除失败');
        }
        $this->error('非法访问');
    }

    // 删除
    public function level_type_del()
    {
        $type_id = input('type_id/d');
        if (IS_AJAX_POST && !empty($type_id)) {
            $where = [
                'type_id' => $type_id,
            ];
            $type_name_list = $this->users_type_manage_db->where($where)->getField('type_name');
            // 删除会员升级级别
            $return = $this->users_type_manage_db->where($where)->delete();
            if ($return) {
                adminLog('删除会员升级级别：' . $type_name_list);
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数有误');
    }

    // 新增/修改
    public function add_level_data()
    {
        if (IS_POST) {
            $post = input('post.');

            // 处理新增数据
            $AddLevelData = [];
            foreach ($post['type_name'] as $key => $value) {
                $type_id = $post['type_id'][$key];
                $type_name = trim($value);
                $level_id = $post['level_id'][$key];
                $price = $post['price'][$key];
                $limit_id = $post['limit_id'][$key];
                $sort_order = $post['sort_order'][$key];

                if (empty($type_name)) $this->error('产品名称不可为空');
                if (empty($level_id)) $this->error('会员级别不可为空');
                if (empty($price)) $this->error('产品价格不可为空');
                if (empty($limit_id)) $this->error('会员期限不可为空');

                $AddLevelData[] = [
                    'type_id' => $type_id,
                    'type_name' => $type_name,
                    'level_id' => $level_id,
                    'price' => $price,
                    'limit_id' => $limit_id,
                    'sort_order' => $sort_order,
                    'update_time' => getTime(),
                ];

                if (empty($type_id)) {
                    $AddLevelData[$key]['add_time'] = getTime();
                    unset($AddLevelData[$key]['type_id']);
                }
            }

            if (!empty($AddLevelData)) {
                $ReturnId = model('UsersTypeManage')->saveAll($AddLevelData);
            }

            // 返回
            if (!empty($ReturnId)) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }
    }

    // 级别列表
    public function level_index()
    {
        $list = array();
        $keywords = input('keywords/s');

        $condition = array();
        // 应用搜索条件
        if (!empty($keywords)) {
            $condition['a.level_name'] = array('LIKE', "%{$keywords}%");
        }

        /**
         * 数据查询
         */
        $count = $this->users_level_db->alias('a')->where($condition)->count();// 查询满足要求的总记录数
        $Page = new Page($count, config('paginate.list_rows'));// 实例化分页类 传入总记录数和每页显示的记录数
        $list = $this->users_level_db->field('a.*')
            ->alias('a')
            ->where($condition)
            ->order('a.level_value asc, a.level_id asc')
            ->limit($Page->firstRow . ',' . $Page->listRows)
            ->select();

        $show = $Page->show();// 分页显示输出
        $this->assign('page', $show);// 赋值分页输出
        $this->assign('list', $list);// 赋值数据集
        $this->assign('pager', $Page);// 赋值分页集

        // 用于判断是否可以删除会员级别，当会员级别下存在会员时，不可删除。
        $levelgroup = $this->users_db->field('level')->group('level')->getAllWithIndex('level');
        $this->assign('levelgroup', $levelgroup);

        return $this->fetch();
    }

    // 级别 - 新增
    public function level_add()
    {
        if (IS_POST) {
            $post = input('post.');
            $post['level_name'] = trim($post['level_name']);

            if (empty($post['level_name'])) $this->error('级别名称不可为空！');
            // if (empty($post['level_value'])) $this->error('会员等级值不可为空！');

            $level_name_count = Db::name('users_level')->where('level_name', $post['level_name'])->count();
            // 级别名称不可重复
            if (!empty($level_name_count)) {
                $this->error('级别名称不可重复！');
            }
            $max_level_value = Db::name('users_level')->max('level_value');
//            $level_value_count = Db::name('users_level')->where('level_value',$post['level_value'])->count();
//            // 会员等级值不可重复
//            if (!empty($level_value_count)) {
//                $this->error('会员等级值不可重复！');
//            }

            $post['level_value'] = $max_level_value + 50;
            $post['add_time'] = getTime();
            $post['update_time'] = getTime();

            $ReturnId = Db::name('users_level')->insert($post);
            if ($ReturnId) {
                \think\Cache::clear('users_level');
                adminLog("新增会员级别：{$post['level_name']}");
                $this->success('操作成功', url('Level/level_index'));
            } else {
                $this->error('操作失败');
            }
        }

        return $this->fetch();
    }

    // 级别 - 新增
    public function level_edit()
    {
        if (IS_POST) {
            $post = input('post.');
            $post['level_name'] = trim($post['level_name']);

            if (empty($post['level_name'])) $this->error('级别名称不可为空！');
            // if (empty($post['level_value'])) $this->error('会员等级值不可为空！');

            $level_name_count = Db::name('users_level')->where('level_name', $post['level_name'])->where('level_id', 'neq', $post['level_id'])->count();
            // 级别名称不可重复
            if (!empty($level_name_count)) {
                $this->error('级别名称不可重复！');
            }
//            $level_value_count = Db::name('users_level')->where('level_value',$post['level_value'])->where('level_id','neq',$post['level_id'])->count();
//            // 会员等级值不可重复
//            if (!empty($level_value_count)) {
//                $this->error('会员等级值不可重复！');
//            }
            $post['update_time'] = getTime();
            $res = Db::name('users_level')->where('level_id', $post['level_id'])->update($post);
            if ($res !== false) {
                \think\Cache::clear('users_level');
                adminLog("编辑会员级别：{$post['level_name']}");
                $this->success('操作成功', url('Level/level_index'));
            } else {
                $this->error('操作失败');
            }
        }
        $level_id = input('param.level_id/d');
        $info = Db::name('users_level')->where('level_id', $level_id)->find();
        if (empty($info)) {
            $this->error('会员级别不存在');
        }
        $this->assign('info', $info);
        return $this->fetch();
    }

    // 级别 - 删除
    public function level_del()
    {
        $level_id = input('del_id/a');
        $level_id = eyIntval($level_id);

        if (IS_AJAX_POST && !empty($level_id)) {
            // 查询条件
            $where = [
                'level_id' => ['IN', $level_id],
            ];
            // 查询会员级别
            $result = $this->users_level_db->field('level_name,is_system,level_value')->where($where)->select();
            $level_name_list = get_arr_column($result, 'level_name');
            // 系统内置级别不可删除
            foreach ($result as $val) {
                if (1 == intval($val['is_system'])) {
                    $this->error('系统内置，不可删除！');
                }
            }
            // 有使用的会员不可删除
            $info = $this->users_db->where([
                'level' => ['IN', $level_id],
            ])->count();
            if (!empty($info)) {
                $this->error('选中的级别存在会员，不可删除！');
            }
            // 删除指定级别
            $return = $this->users_level_db->where($where)->delete();
            if ($return) {
                // 查询指定会员级别
                $where1 = [
                    'level_value' => ['>', $result[0]['level_value']],
                ];
                $result_1 = $this->users_level_db->where($where1)->order('level_value asc')->field('level_id')->find();
                if (empty($result_1)) {
                    $where1 = [
                        'level_value' => ['<', $result[0]['level_value']],
                    ];
                    $result_1 = $this->users_level_db->where($where1)->order('level_value asc')->field('level_id')->find();
                }
                // 拼装更新条件
                $UpData = [
                    'level_id' => $result_1['level_id'],
                    'update_time' => getTime(),
                ];
                // 更新会员升级表数据
                Db::name('users_type_manage')->where($where)->update($UpData);
                \think\Cache::clear('users_level');
                adminLog('删除会员级别：' . implode(',', $level_name_list));
                $this->success('删除成功');
            } else {
                $this->error('删除失败');
            }
        }
        $this->error('参数有误');
    }
}